home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Games / dynAMIte / Developer / C / BotFrame.c next >
C/C++ Source or Header  |  2001-06-24  |  2KB  |  128 lines

  1. #include    <stdlib.h>
  2. #include     <stdio.h>
  3. #include    <time.h>
  4.  
  5. #include     <exec/semaphores.h>
  6. #include     <dos/dos.h>
  7. #include     <proto/exec.h>
  8. #include     <proto/dos.h>
  9.  
  10. #include     "dynamite.h"
  11.  
  12.  
  13. BOOL        checkDynamite (struct dynamitesemaphore ** );
  14. void        mainLoop (struct dynamitesemaphore *);
  15. void        doAI (struct dynamitesemaphore *);
  16.  
  17.  
  18. int
  19. main (void)
  20. {
  21.     struct dynamitesemaphore    *dynasema;
  22.  
  23.  
  24.     srand (time(NULL));
  25.  
  26.     if ( FALSE == checkDynamite (&dynasema) )
  27.     {
  28.         puts ("*** DynAMIte not running!");
  29.         return EXIT_FAILURE;
  30.     }
  31.  
  32.     /* some information */
  33.     printf ("Number of clients: %Ld\n",dynasema->opencnt);
  34.  
  35.     /* enter main loop */
  36.     mainLoop (dynasema);
  37.     
  38.     /* decrease opencount to tell dynamite that you no
  39.     longer need the semaphore. dynamite will remove the
  40.     semaphore only if opencnt is 0 at the end */
  41.     ObtainSemaphore(&dynasema->sema);
  42.     dynasema->opencnt--;
  43.     ReleaseSemaphore(&dynasema->sema);
  44.  
  45.     return EXIT_SUCCESS;
  46. }
  47.  
  48. BOOL
  49. checkDynamite (struct dynamitesemaphore ** dynasema )
  50. {
  51.     Forbid();    /* try to find the semaphore */
  52.     if (*dynasema = (struct dynamitesemaphore *)FindSemaphore ("dynAMIte.0")
  53.         {
  54.         /* increase opencount to tell dynamite that you are using the
  55.         semaphore. dynamite will remove semaphore only if opencnt is
  56.         0 at its end */
  57.  
  58.         ++ dynasema->opencnt;
  59.         }
  60.     Permit();
  61.  
  62.     if ( NULL == *dynasema )
  63.         return FALSE;
  64.  
  65.     return TRUE;
  66. }
  67.  
  68. void
  69. mainLoop (struct dynamitesemaphore * dynasema)
  70. {
  71.     struct player                 *ourplayer;
  72.     BOOL                        done = FALSE,
  73.                                 delay;
  74.  
  75.     while (FALSE == done)
  76.     {
  77.         if (CheckSignal (SIGBREAKF_CTRL_C))
  78.             done = TRUE;
  79.         else
  80.         {
  81.             delay = TRUE;
  82.             ObtainSemaphore (&dynasema->sema);
  83.  
  84.             if (dynasema->quit)
  85.             {
  86.                 /* dynamite wants to quit, so we do dynamite a favour */
  87.                 printf ("dynAMIte is about to quit...\n");
  88.                 done = TRUE;
  89.             }
  90.             else
  91.             {
  92.                 /* if a game is running */
  93.                 if (dynasema->gamerunning>=GAME_GAME)
  94.                 {
  95.                     /* and player is no observer */
  96.                     if (dynasema->thisplayer < 8)
  97.                     {
  98.                         delay = FALSE;
  99.  
  100.                         ourplayer = dynasema->players[dynasema->thisplayer];
  101.  
  102.                         /* and our player is alive */
  103.                         if (ourplayer->dead)
  104.                             doAI (dynasema);
  105.                     }
  106.                 }
  107.             }
  108.  
  109.             ReleaseSemaphore (&dynasema->sema);
  110.  
  111.             if (delay)
  112.             {
  113.                 /* no game is running */
  114.                 /* do a small delay to let the cpu do other things :( */
  115.                 Delay (10);
  116.             }
  117.  
  118.         }
  119.     }
  120. }
  121.  
  122. void
  123. doAI (struct dynamitesemaphore * dynasema)
  124. {
  125.     dynasema->walk = rand() % DIR_UP+1;
  126. }
  127.  
  128.